package com.yeskery.nut.web.demo.event;

import com.yeskery.nut.annotation.async.Async;
import com.yeskery.nut.annotation.bean.Autowired;
import com.yeskery.nut.annotation.bean.Component;
import com.yeskery.nut.annotation.event.EventListener;
import com.yeskery.nut.web.demo.domain.User;
import com.yeskery.nut.web.demo.domain.Wallet;
import com.yeskery.nut.web.demo.service.WalletService;

import java.util.logging.Logger;

/**
 * 用户注册事件监听器
 * @author Yeskery
 * 2023/8/9
 */
@Component
public class UserRegisterEventListener {

    /** 日志对象 */
    private static final Logger logger = Logger.getLogger(UserRegisterEventListener.class.getName());

    @Autowired
    private WalletService walletService;

    /**
     * 用户注册事件的处理方法
     */
    @Async
    @EventListener(UserRegisterEvent.class)
    public void handleUserRegisterEvent(UserRegisterEvent event) {
        User user = (User) event.getSource();
        // 赠送用户100积分
        walletService.lambdaUpdate().eq(Wallet::getUserId, user.getId()).set(Wallet::getPoint, 100).update();

        logger.info("赠送用户id: " + user.getId() + "，100积分");
    }
}
